<template>
  <div class="black-user-manage">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>黑名单用户管理</span>
        </div>
      </template>
      
      <div class="search-box">
        <el-button type="primary" @click="refreshData">刷新</el-button>
      </div>
      
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column label="用户信息" min-width="200">
          <template #default="scope">
            <div class="user-info">
              <el-avatar :size="40" :src="scope.row.user.avatar || '/avatar.jpg'" />
              <div class="user-detail">
                <div>{{ scope.row.user.name }}</div>
                <div class="user-id">ID: {{ scope.row.userId }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="学号" min-width="150">
          <template #default="scope">
            {{ scope.row.user?.studentId || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="手机号" min-width="150">
          <template #default="scope">
            {{ scope.row.user?.phone || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="加入黑名单时间" min-width="180" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="scope">
            <el-button
              type="danger"
              size="small"
              @click="handleRemoveFromBlacklist(scope.row)"
            >
              移出黑名单
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getBlackUserList, removeFromBlacklist } from '@/api/admin/blackUser'

const loading = ref(false)
const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取黑名单用户列表
const fetchData = async () => {
  loading.value = true
  try {
    const params = {
      current: currentPage.value,
      size: pageSize.value
    }
    const res = await getBlackUserList(params)
    if (res.code === 200) {
      tableData.value = res.data.records
      total.value = res.data.total
    }
  } catch (error) {
    console.error('获取黑名单用户列表失败:', error)
    ElMessage.error('获取黑名单用户列表失败')
  } finally {
    loading.value = false
  }
}

// 刷新数据
const refreshData = () => {
  fetchData()
}

// 移出黑名单
const handleRemoveFromBlacklist = (row) => {
  ElMessageBox.confirm(`确认将用户 ${row.user?.name || row.userId} 移出黑名单吗？`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await removeFromBlacklist(row.userId)
      if (res.code === 200) {
        ElMessage.success('移出黑名单成功')
        fetchData()
      } else {
        ElMessage.error(res.msg || '操作失败')
      }
    } catch (error) {
      console.error('移出黑名单失败:', error)
      ElMessage.error('移出黑名单失败')
    }
  }).catch(() => {})
}

// 处理页码变化
const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchData()
}

// 处理每页条数变化
const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.black-user-manage {
  padding: 20px;
}

.search-box {
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-end;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-detail {
  margin-left: 10px;
}

.user-id {
  font-size: 12px;
  color: #999;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style> 